package com.serviceA.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.rabbitmq.tools.json.JSONUtil;
import com.serviceA.entity.WorkInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lwj
 * @create 2021/6/26 15:38
 */
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ConfirmController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendConfirm/{key}")
    public void sendConfirmMessage(@RequestParam(value = "key",defaultValue = "confirm-key")String key) throws JsonProcessingException {
        /**声明回调的形参*/
        CorrelationData correlationData = new CorrelationData("1");
        WorkInfo workInfo = new WorkInfo("123", "tom", "内容");
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writer();
        String jsonString = writer.writeValueAsString(workInfo);
        rabbitTemplate.convertAndSend("confirm-exchange", key, jsonString,correlationData);
        log.info("发送信息为:" + jsonString);
    }
}
